<?php
class Application_Model_DbTable_Bet extends Zend_Db_Table_Abstract
{
    protected $_name = 'bet';
	protected $_referenceMap = array(
		'User' => array(
			'columns' => 'user_id',
			'refTableClass' => 'Application_Model_DbTable_User',
			'refColumns' => array('id')
		)
	);
    public function GetAllBets(){
    	return 0;
    }
    public function CheckBetExists($event_id, $user_id){
    	$select = $this->select();
    	$select->where('event_id = ?', $event_id)->where('user_id = ?', $user_id);
    	$res = $this->fetchRow($select);
    	if($res){
    		return $res->id;
    	}
    	else{
    		return 0;
    	}
    }
    public function SaveBet($event_id, $user_id, $bet_left, $bet_right){
    	if($bet_left > $bet_right){
    		$bet_result = 'left';
    	}
    	elseif($bet_left < $bet_right){
    		$bet_result = 'right';
    	}
    	else{
    		$bet_result = 'draw';
    	}
    	$exists = $this->CheckBetExists($event_id, $user_id);
    	$data = array('event_id' => $event_id, 
    					'user_id' => $user_id, 'bet_left' => $bet_left, 
    					'bet_right' => $bet_right, 'bet_result' => $bet_result);
    	if($exists){
    		$this->update($data, 'id = ' . $exists);
    	}
    	else{
    		$this->insert($data);
    	}
    }
    
}
